package com.ruoyi.ewem.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ewem.config.QualityDataListener;
import com.ruoyi.ewem.domain.*;
import com.ruoyi.ewem.domain.Process;
import com.ruoyi.ewem.service.*;
import com.ruoyi.ewem.domain.Quality;
import com.ruoyi.ewem.service.QualityService;
import com.ruoyi.ewem.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("/quality")
public class QualityController extends BaseController {
    @Autowired
    QualityService qualityService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    MinioUtils minioUtils;
    @Autowired
    ProductService productService;
    @Autowired
    FirmService firmService;
    @Autowired
    DriversService driversService;
    @Autowired
    GoodsMaterialService goodsMaterialService;
    @Autowired
    ProcessService processService;

    /*
    * todo 列表条件查询
    */
    @RequestMapping("list")
    public TableDataInfo listQuality(Quality quality) {
        startPage();
        List<Quality> list = qualityService.lists(quality);
        return getDataTable(list);
    }
    @GetMapping("lists")
    public TableDataInfo prodList(){
        List<Product> list = productService.list();
        return getDataTable(list);
    }
    @GetMapping("proclist")
    private TableDataInfo proclist(){
        List<Process> list = processService.list();
        return getDataTable(list);
    }
    /*
     * todo 查询根据id查找对应信息
     */
    @GetMapping("/getProduct/{productId}")
    public AjaxResult getProduct(@PathVariable Integer productId){
        Product product = productService.getByIds(productId);
        return success(product);
    }
    @GetMapping("/getFirm/{firmId}")
    public AjaxResult getFirm(@PathVariable Integer firmId){
        Firm firm = firmService.getByIds(firmId);
        return success(firm);
    }
    @GetMapping("getDricer/{dricerId}")
    public AjaxResult getDricer(@PathVariable Integer dricerId){
        Drivers byId = driversService.getById(dricerId);
        return success(byId);
    }
    @GetMapping("getGoods/{goodsMaterialId}")
    public AjaxResult getGoods(@PathVariable Integer goodsMaterialId){
        GoodsMaterial material = goodsMaterialService.getByIds(goodsMaterialId);
        return success(material);
    }

    /*
    todo 下拉查询
    */
    @RequestMapping("productList")
    public TableDataInfo productList(){
        List<Product> list = productService.list();
        return getDataTable(list);
    }
    @RequestMapping("firmList")
    public TableDataInfo firmList(){
        List<Firm> list = firmService.list();
        return getDataTable(list);
    }
    @RequestMapping("driversList")
    public TableDataInfo driversList(){
        List<Drivers> list = driversService.list();
        return getDataTable(list);
    }
    @RequestMapping("goodsMaterialList")
    public TableDataInfo goodsMaterialList(){
        List<GoodsMaterial> list = goodsMaterialService.list();
        return getDataTable(list);
    }

    /*
     * todo 添加
     */
    @RequestMapping("add")
    public AjaxResult addQuality(@RequestBody Quality quality){
        Boolean absent = redisTemplate.opsForValue().setIfAbsent(quality.hashCode() + "", quality, 1, TimeUnit.MINUTES);
        if (!absent){
            return AjaxResult.error("请勿重复提交");
        }
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getProductId,quality.getProductId());
        Product one = productService.getOne(wrapper);
        quality.setBatchNum(one.getBatchNum());
        quality.setQualityName(SecurityUtils.getUsername());
        quality.setQualityTime(new Date());
        quality.setCreateBy(SecurityUtils.getUsername());
        quality.setCreateTime(new Date());
        boolean b = qualityService.save(quality);
        return toAjax(b);
    }

    /*
     * todo 修改
     */
    @RequiresPermissions("ewem:quality:updates")
    @RequestMapping("updates")
    public AjaxResult updateQuality(@RequestBody Quality quality){
        quality.setUpdateBy(SecurityUtils.getUsername());
        quality.setUpdateTime(new Date());
        boolean b = qualityService.updateById(quality);
        return toAjax(b);
    }

    @RequestMapping("zhijian")
    public AjaxResult updaye(@RequestBody Quality quality){
        quality.setUpdateBy(SecurityUtils.getUsername());
        quality.setUpdateTime(new Date());
        boolean b = qualityService.updateById(quality);
        return toAjax(b);
    }

    /*
     * todo 删除
     */
    @RequiresPermissions("ewem:quality:dels")
    @RequestMapping("dels/{qualityIds}")
    public AjaxResult delQuality(@PathVariable(value = "qualityIds") Integer[] qualityIds){
        return toAjax(qualityService.removeByIds(Arrays.asList(qualityIds)));
    }

    /**
     * 导出质检列表
     */
    @RequestMapping("upload")
    public String upload(MultipartFile file) throws Exception {
        String upload = minioUtils.upload(file);
        return upload;
    }
    /*
     * todo 导出
     */
    @RequestMapping("export")
    public void export(HttpServletResponse response){
        List<Quality> list = qualityService.list();
        ExcelUtil<Quality> util = new ExcelUtil<>(Quality.class);
        util.exportExcel(response,list,"质检数据");
    }
    /*
     * todo 导入数据
     */
    @RequestMapping("importUploads")
    public AjaxResult importUploads(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),Quality.class,new QualityDataListener(qualityService)).sheet().doRead();
        return success();
    }
}
